class Solution(object): def generate(self, numRows): """ :type numRows: int :rtype: List[List[int]] """ if numRows == 0: return [] ans = [] for i in range(numRows): row = [] for j in range(i+1): row.append(1) if i > 1: for x in range(i - 1): row[x+1] = ans[i-1][x] + ans[i-1][x+1] print row ans.append(row) return ans
class Solution(object): def getRow(self, rowIndex): """ :type rowIndex: int :rtype: List[int] """ rowIndex += 1 ans = [] if rowIndex == 0: return [] for i in range(rowIndex): this = [] for j in range(i+1): this.append(1) if i > 1: for x in range(i - 1): this[x+1] = ans[i-1][x] + ans[i-1][x+1] print this ans.append(this) return ans[rowIndex - 1]
总结
帕斯卡三角, 我自己一开始试着写了一个函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
def get_num(i, j): # 三种特例,左边右边最上面 if i == j: return 1 if i == 0: return 1 if i > 0 and j == 0: return 1 # 输入有误返回False if i < j: return False else: return get_num(i - 1, j - 1) + get_num(i - 1, j)